<template>
  <div style="frot-size: 12px; line-height: 60px; display: flex">
    <div style="flex: 1; font-size: 18px">
      <span :class="collapseBtnClass" style="cursor: pointer; font-size: 15px" @click="collapse"/>
      <el-breadcrumb separator="/" style="display: inline-block; margin-left: 10px">
      <el-breadcrumb-item v-for="(item,index) in lists" :key="item.path">        <!--     遍历路由信息并展示-->
        <router-link :to="item.path">{{item.meta.title}}</router-link>
      </el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <el-dropdown style="cursor: pointer">
      <div style="display: inline-block;">
        <el-avatar @error="errorHandler"
                   style="width: 35px; border-radius: 50%; position: relative; top:10px; right: 5px; height: 35px">
          <img :src="user.avatarUrl" alt="" style="width: 35px; height: 35px">
        </el-avatar>
        <span>{{ user.nickname }}</span>
        <i class="el-icon-arrow-down ml-5" ></i>
      </div>
      <el-dropdown-menu slot="dropdown" style="width: 100px;text-align: center">
        <el-dropdown-item style="font-size: 14px; padding: 5px 0">
          <p @click="toPerson">个人信息</p>
        </el-dropdown-item>
        <el-dropdown-item style="font-size: 14px; padding: 5px 0">
          <p @click="toFront">前端</p>
        </el-dropdown-item>
        <el-dropdown-item>
          <span style="text-decoration: none" @click="logout">退出</span>
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </div>
</template>

<style>
</style>

<script>
import {resetRouter} from "@/router";

export default {
  name: "Header",
  props: {
    collapseBtnClass: String,
    collapse: Function,
    user: Object
  },

  data() {
    return {
      lists:[], ////定义一个数组 用于接收路由信息
    }

  },
  created() {
    // console.log(this.$route.matched)
    this.lists = this.$route.matched  //获取路由内的全部信息
  },
  //这里必须使用监听，否则无法实时获取路由变动信息。
  //监听后路由会实时变动，不然需要手动刷新路径才会改变
  watch:{
    $route(to,from)  {
      // console.log(to)
      this.lists = to.matched
    }
  },
  methods: {
    logout() {
      localStorage.removeItem("user")
      localStorage.removeItem("menus")
      // 重置路由
      resetRouter()
      this.$router.push("/login")
      this.$message.success("退出成功")
    },
    errorHandler() {
      return true
    },
    toPerson() {
      this.$router.push("/person")
    },
    toFront() {
      this.$router.push("/front")
    }
  }


}
</script>